package com.softservecom.training.shop.servlets;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Blob;
import java.util.List;
import java.util.Set;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.softservecom.training.shop.dao.ImagesDAO;
import com.softservecom.training.shop.dao.ProductsCategoriesDAO;
import com.softservecom.training.shop.pojos.Images;
import com.softservecom.training.shop.pojos.Products;
import com.softservecom.training.shop.pojos.ProductsCategories;

public class ImagesS extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public ImagesS() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {


		RequestDispatcher dispatcher = getServletContext()
				.getRequestDispatcher("/UploadImages.jsp");

		dispatcher.forward(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String contentType = request.getContentType ();

        if ((contentType != null) && (contentType.indexOf ("multipart/form-data") >= 0)) {

            DataInputStream in = new DataInputStream (request.getInputStream ());

            int formDataLength = request.getContentLength ();

           
   byte dataBytes[] = new byte[formDataLength];

           

         

            int byteRead = 0;

            int totalBytesRead = 0;

            while (totalBytesRead < formDataLength) {

                byteRead = in.read (dataBytes, totalBytesRead, formDataLength);

                totalBytesRead += byteRead;

            }

            String file = new String (dataBytes);

       
         String saveFile = file.substring (file.indexOf ("filename=\"") + 10);


            saveFile = saveFile.substring (0, saveFile.indexOf ("\n"));

            saveFile = saveFile.substring (saveFile.lastIndexOf ("\\") + 1,saveFile.indexOf ("\""));

            int lastIndex = contentType.lastIndexOf ("=");
           String boundary = contentType.substring (lastIndex + 1,contentType.length ());

           

            int pos;

            pos = file.indexOf ("filename=\"");

           

            pos = file.indexOf ("\n", pos) + 1;


            pos = file.indexOf ("\n", pos) + 1;

            pos = file.indexOf ("\n", pos) + 1;

            int boundaryLocation = file.indexOf (boundary, pos) - 4;

            int startPos = ((file.substring (0, pos)).getBytes ()).length;

            int endPos = ((file.substring (0, boundaryLocation)).getBytes ()).length;

            String[] fileName = saveFile.split ("\\.");

            String file_extension = fileName[1];

           String imageName = "D:\\SoftServe\\InternetShop\\src\\Test."+file_extension ;

           

   

            FileOutputStream fileOut = new FileOutputStream (imageName);

            fileOut.write (dataBytes, startPos, (endPos - startPos));

            fileOut.flush ();
            fileOut.close ();
        }
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
